import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  // /admin 放在 / 前面优先判断，不然当进入管理员界面时会优先匹配到 / 中的错误页面
  // 后台登陆
  {
    path: '/admin/login',
    name: 'adminLogin',
    component: () => import('@/views/admin/login/LoginView.vue')
  },
  // 后台路由
  {
    path: '/admin',
    name: 'admin',
    component: () => import('@/views/admin/layout/LayoutView.vue'),
    children: [
      {
        path: '',
        redirect: '/admin/home'
      },
      {
        path: '/admin/home',
        name: 'adminHome',
        component: () => import('@/views/admin/layout/HomeView.vue')
      },
      {
        path: '/admin/sightsManage',
        name: 'sightsManage',
        component: () => import('@/views/admin/sights/SightsManage.vue')
      },
      {
        path: '/admin/sightsBooking',
        name: 'sightsBooking',
        component: () => import('@/views/admin/sights/SightsBooking.vue')
      },
      {
        path: '/admin/routeManage',
        name: 'routeManage',
        component: () => import('@/views/admin/route/RouteManage.vue')
      },
      {
        path: '/admin/hotelManage',
        name: 'hotelManage',
        component: () => import('@/views/admin/hotel/HotelManage.vue')
      },
      {
        path: '/admin/hotel/specs/:id',
        name: 'hotelSpecs',
        component: () => import('@/views/admin/hotel/HotelSpecs.vue'),
        props: true
      },
      {
        path: '/admin/hotelBooking',
        name: 'hotelBooking',
        component: () => import('@/views/admin/hotel/HotelBooking.vue')
      },
      {
        path: '/admin/infoManage',
        name: 'infoManage',
        component: () => import('@/views/admin/information/InfoManage.vue')
      },
      {
        path: '/admin/bannerManage',
        name: 'BannerManage',
        component: () => import('@/views/admin/banner/BannerManage.vue')
      },
      {
        path: '/admin/privateInfo',
        name: 'privateInfo',
        component: () => import('@/views/admin/user/PrivateInfo.vue')
      },
      {
        path: '/admin/userManage',
        name: 'userManage',
        component: () => import('@/views/admin/user/UserManage.vue')
      },
      {
        path: '/admin/adminManage',
        name: 'adminManage',
        component: () => import('@/views/admin/user/AdminManage.vue')
      },
      // 错误页面
      {
        path: '/admin/error',
        name: 'adminError',
        component: () => import('@/components/ErrorView.vue')
      },
      {
        path: '/admin/*',
        redirect: '/admin/error'
      }
    ]
  },
  // 景点二维码处理
  {
    path: '/sights/booking/qr/:id',
    name: 'sightsBookingQR',
    component: () => import('@/views/client/booking/SightsBookingQRUse.vue'),
    props: true
  },
  // 酒店二维码处理
  {
    path: '/hotel/booking/qr/:id',
    name: 'hotelBookingQR',
    component: () => import('@/views/client/booking/HotelBookingQRUse.vue'),
    props: true
  },
  // 前台登陆
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/client/login/LoginView.vue')
  },
  // 忘记密码
  {
    path: '/forget',
    name: 'forget',
    component: () => import('@/views/client/login/ForgotView.vue')
  },
  // 前台路由
  {
    path: '/layout',
    name: 'layout',
    component: () => import('@/views/client/layout/LayoutView.vue'),
    children: [
      {
        path: '',
        redirect: '/home'
      },
      {
        path: '/home',
        name: 'home',
        component: () => import('@/views/client/layout/HomeView.vue')
      },
      {
        path: '/sights',
        name: 'sights',
        component: () => import('@/views/client/sights/SightsView.vue')
      },
      {
        path: '/sightsDetail/:id',
        name: 'sightsDetail',
        component: () => import('@/views/client/sights/SightsDetail.vue'),
        props: true
      },
      {
        path: '/route',
        name: 'route',
        component: () => import('@/views/client/route/RouteView.vue')
      },
      {
        path: '/routeDetail/:id',
        name: 'routeDetail',
        component: () => import('@/views/client/route/RouteDetail.vue'),
        props: true
      },
      {
        path: '/hotel',
        name: 'hotel',
        component: () => import('@/views/client/hotel/HotelView.vue')
      },
      {
        path: '/hotelDetail/:id',
        name: 'hotelDetail',
        component: () => import('@/views/client/hotel/HotelDetail.vue'),
        props: true
      },
      {
        path: '/info',
        name: 'info',
        component: () => import('@/views/client/information/InfoView.vue')
      },
      {
        path: '/infoDetail/:id',
        name: 'infoDetail',
        component: () => import('@/views/client/information/InfoDetail.vue'),
        props: true
      },
      {
        path: '/booking/sights/detail/:id',
        name: 'sightsBookingDetail',
        component: () =>
          import('@/views/client/booking/SightsBookingDetail.vue'),
        props: true
      },
      {
        path: '/booking/hotel/detail/:id',
        name: 'hotelBookingDetail',
        component: () =>
          import('@/views/client/booking/HotelBookingDetail.vue'),
        props: true
      },
      {
        path: '/booking/sights/use/:id',
        name: 'sightsBookingUse',
        component: () => import('@/views/client/booking/SightsBookingUse.vue'),
        props: true
      },
      {
        path: '/booking/hotel/use/:id',
        name: 'HotelBookingUse',
        component: () => import('@/views/client/booking/HotelBookingUse.vue'),
        props: true
      },
      {
        path: '/private',
        name: 'private',
        component: () => import('@/views/client/user/Private.vue'),
        children: [
          {
            path: '',
            redirect: '/private/route'
          },
          {
            path: '/private/route',
            name: 'privateRoute',
            component: () => import('@/views/client/user/Route.vue')
          },
          {
            path: '/private/collect',
            name: 'privateCollect',
            component: () => import('@/views/client/user/Collect.vue')
          },
          {
            path: '/private/like',
            name: 'privateLike',
            component: () => import('@/views/client/user/Like.vue')
          },
          {
            path: '/private/booking',
            name: 'privateBooking',
            component: () => import('@/views/client/user/Booking.vue')
          },
          {
            path: '/private/setting',
            name: 'Setting',
            component: () => import('@/views/client/user/Setting.vue')
          }
        ]
      },
      {
        path: '/other',
        name: 'other',
        component: () => import('@/views/client/other/OtherPrivate.vue'),
        props: true,
        redirect: (to) => {
          return `/other/route/${to.params.id}`
        },
        children: [
          {
            path: '/other/route/:id',
            name: 'otherRoute',
            component: () => import('@/views/client/other/OtherRoute.vue'),
            props: true
          },
          {
            path: '/other/collect/:id',
            name: 'otherCollect',
            component: () => import('@/views/client/other/OtherCollect.vue'),
            props: true
          },
          {
            path: '/other/like/:id',
            name: 'otherLike',
            component: () => import('@/views/client/other/OtherLike.vue'),
            props: true
          },
          {
            path: '/other/booking/:id',
            name: 'otherBooking',
            component: () => import('@/views/client/other/OtherBooking.vue'),
            props: true
          }
        ]
      },
      // 错误页面
      {
        path: '/error',
        name: 'userError',
        component: () => import('@/components/ErrorView.vue')
      },
      {
        path: '/*',
        redirect: '/error'
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 使页面跳转时会回到顶部，不会停留在上次浏览的位置（仅限新页面跳转，同页面请求后不会回到顶部）
router.afterEach((to, from, next) => {
  if (
    !(
      to.name === 'privateRoute' ||
      to.name === 'privateCollect' ||
      to.name === 'privateLike' ||
      to.name === 'privateBooking' ||
      to.name === 'Setting' ||
      to.name === 'otherRoute' ||
      to.name === 'otherCollect' ||
      to.name === 'otherLike' ||
      to.name === 'otherBooking'
    )
  ) {
    window.scrollTo(0, 0)
  }
})

// 解决Vue报错：Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch((err) => err)
}

export default router
